package com.suarte.reports;

import com.lowagie.text.*;
import com.suarte.core.Invoice;
import java.io.IOException;
import java.io.OutputStream;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.suarte.core.InvoiceDetail;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @date   Mar 12, 2011
 * @author Ggutierrez
 */
public class InvoiceFormatToPdf {

    Image logo;
    Format formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    Format simpleFormat = new SimpleDateFormat("dd/MM/yyyy");

    public InvoiceFormatToPdf() {
        try {
            logo = Image.getInstance(getClass().getResource("/suarte_logo.png"));
        } catch (BadElementException ex) {
            Logger.getLogger(InvoiceFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(InvoiceFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(InvoiceFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write(Invoice invoice, OutputStream out) {
        Document document = new Document(PageSize.LETTER.rotate());
        try {
            // step 2:
            // we create a writer that listens to the document and directs a PDF-stream to System.out (and a txt file)
            PdfWriter.getInstance(document, out);
            // step 3: we open the document
            document.open();

            document.add(blankSpace());
            document.add(blankSpace());
            document.add(blankSpace());

            addHeader(document, invoice);

            document.add(blankSpace());
            document.add(blankSpace());

            addDetail(document, invoice);

            document.close();

        } catch (DocumentException ex) {
            Logger.getLogger(InvoiceFormatToPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addHeader(Document document, Invoice invoice) throws DocumentException {
        //Table size 2 columns
        float[] widths = {0.6f, 0.3f};
        PdfPTable header = new PdfPTable(widths);

        header.setWidthPercentage(90);
        header.getDefaultCell().setBorder(0);

        header.addCell(new Paragraph(""));

        String invoiceDate = simpleFormat.format(invoice.getDate());
        Paragraph p = new Paragraph(invoiceDate, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_RIGHT);
        header.addCell(p);

        // Add a blank line
        header.addCell(new Paragraph(" "));
        header.addCell(new Paragraph(" "));

        String companyName = invoice.getCompany().getDescription();
        p = new Paragraph(companyName, new Font(Font.HELVETICA, 12, Font.NORMAL));
        p.setAlignment(Element.ALIGN_LEFT);
        header.addCell(p);
        
        header.addCell(new Paragraph(""));
        document.add(header);
    }

    public void addDetail(Document document, Invoice invoice) throws DocumentException {
        //Table size 6 columns: Product, resolution, workType, price, quantity, total
        float[] widths = {0.15f, 0.4f, 0.15f, 0.15f, 0.1f};
        PdfPTable header = new PdfPTable(widths);

        header.setWidthPercentage(90);
        header.getDefaultCell().setBorder(0);
        header.getDefaultCell().setGrayFill(0f);

        for (InvoiceDetail detail : invoice.getDetails()) {
            header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

            Paragraph p = new Paragraph(detail.getQuotationDetail().getQuantity().toString(), new Font(Font.HELVETICA, 11, Font.NORMAL));
            header.addCell(p);

            String description = detail.getQuotationDetail().getProduct() != null ? detail.getQuotationDetail().getProduct().getDescription() : detail.getQuotationDetail().getItemDescription();
            p = new Paragraph(description, new Font(Font.HELVETICA, 11, Font.NORMAL));
            header.addCell(p);

            header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            p = new Paragraph(detail.getQuotationDetail().getPrice().toString(), new Font(Font.HELVETICA, 11, Font.NORMAL));
            header.addCell(p);

            p = new Paragraph(detail.getQuotationDetail().getSubTotal().toString(), new Font(Font.HELVETICA, 11, Font.NORMAL));
            header.addCell(p);

            header.addCell(new Paragraph(""));
        }

        header.getDefaultCell().setColspan(5);
        header.addCell(blankSpace());
        //Footer
        header.getDefaultCell().setColspan(0);
        header.addCell(new Paragraph(""));
        header.addCell(new Paragraph(""));

        String currencySymbol = invoice.getCurrency().getSymbol();
        header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        Paragraph t = new Paragraph(currencySymbol, new Font(Font.HELVETICA, 11, Font.NORMAL));
        header.getDefaultCell().setColspan(0);
        header.addCell(t);

        t = new Paragraph(invoice.getSubTotal().toString(), new Font(Font.HELVETICA, 11, Font.NORMAL));
        header.getDefaultCell().setColspan(0);
        header.addCell(t);

        header.addCell(new Paragraph(""));
        header.addCell(new Paragraph(""));
        header.addCell(new Paragraph(""));
        
        t = new Paragraph(currencySymbol, new Font(Font.HELVETICA, 11, Font.NORMAL));
        header.getDefaultCell().setColspan(0);
        header.addCell(t);

        t = new Paragraph(invoice.getTax().toString(), new Font(Font.HELVETICA, 11, Font.NORMAL));
        header.getDefaultCell().setColspan(0);
        header.addCell(t);
        
        header.addCell(new Paragraph(""));
        header.addCell(new Paragraph(""));
        header.addCell(new Paragraph(""));
        
        t = new Paragraph(currencySymbol, new Font(Font.HELVETICA, 11, Font.NORMAL));
        header.getDefaultCell().setColspan(0);
        header.addCell(t);

        t = new Paragraph(invoice.getTotal().toString(), new Font(Font.HELVETICA, 11, Font.NORMAL));
        header.getDefaultCell().setColspan(0);
        header.addCell(t);

        header.addCell(new Paragraph(""));

        document.add(header);
    }

    public Paragraph blankSpace() {
        //White Line
        String text = " ";
        Paragraph s = new Paragraph(text);
        s.setAlignment(Element.ALIGN_CENTER);
        return s;
    }
}
